package com.golden.order02.service;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Properties;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.order02.dao.OOrderMapper;
import com.golden.order02.dao.OrderDocumentMapper;
import com.golden.order02.dao.OrderHisSalesMapper;
import com.golden.order02.domain.DocumentDO;
import com.golden.order02.domain.ResultDO;
import com.golden.order02.entity.OOrder;
import com.golden.order02.entity.OrderDocument;
import com.golden.order02.entity.OrderHisSales;
import com.golden.order02.query.OrderDocumentExample;
import com.golden.order02.query.OrderDocumentExample.Criteria;
import com.golden.order02.queryDO.DocumentQueryDO;
import com.golden.order02.util.ImageUtils;

import sun.misc.BASE64Decoder;

@Service("orderDocumentService")
public class OrderDocumentServiceImpl implements OrderDocumentService {

	@Autowired
	private OrderDocumentMapper orderDocumentMapper;
	@Autowired
	private OrderHisSalesMapper orderDetailMapper;
	@Autowired
	private OOrderMapper oOrderMapper;

	@Autowired
	private IDocx2PdfService iDocx2PdfService;
	
	private static Logger logger = Logger.getLogger(OrderDocumentServiceImpl.class);


	//@Value("${pdf.pdf.signedPdf}")
	private String signedPdf;
	
	//@Value("${pdf.order.document}")
	private String document1;
	
	//@Value("${pdf.order.source}")
	private String source;
	
	//@Value("${pdf.pdf.keyStorePath}")
	private String keyStorePath;
	
	//@Value("${pdf.pdf.passWord}")
	private String passWord;

	//@Value("${pdf.pdf.sealPicPath}")
	private String sealPicPath;
	
	
	public OrderDocumentServiceImpl() {
		
		InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("applicationOrder.properties");
    	Properties pro=new Properties();
    	try {
			pro.load(inStream);
			signedPdf=pro.getProperty("pdf.pdf.signedPdf");
			document1=pro.getProperty("pdf.order.document");
			source=pro.getProperty("pdf.order.source");
			keyStorePath=pro.getProperty("pdf.pdf.keyStorePath");
			passWord=pro.getProperty("pdf.pdf.passWord");
			sealPicPath=pro.getProperty("pdf.pdf.sealPicPath");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated constructor stub
	}
	
	@Autowired
	IDigitallySigningPdfService iDigitallySigningPdfService;
	
	
	/*public static IFileManagementService getIFileManagementService() {
		return SysFactory.getIFileManagementService();
	}*/

	@Override
	public ResultDO<Integer> add(DocumentDO compact) {
		ResultDO<Integer> result = new ResultDO<Integer>();

		Date date = new Date();
		if (compact == null || compact.getOrderSalesID() == null) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR + "id异常");
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			return result;
		}
		BaseServiceImpl<OrderDocument, DocumentDO> bi = new BaseServiceImpl<>();
		OrderDocument order = new OrderDocument();
		OrderDocument t = bi.getT(compact, order);
		
		t.setIsDelete(0);
		t.setModifitime(date);
		t.setCreatetime(date);
		int insert = -1;
		t.setStatus(compact.getStatus());
		try {
			insert = orderDocumentMapper.insert(t);
		} catch (Exception e) {
			result.setSuccess(false);
			logger.error(ResultDO.ORDER_ADD_MSG_ERROR + "添加异常");
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "添加异常");
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			return result;
		}
		if (insert < 0) {
			result.setSuccess(false);
			logger.error(ResultDO.ORDER_ADD_MSG_ERROR + "添加异常");
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "添加异常");
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
		}
		result.setData(t.getId());
		return result;
	}

	@Override
	public ResultDO<List<DocumentDO>> qeury(DocumentQueryDO query) {

		ResultDO<List<DocumentDO>> result = new ResultDO<List<DocumentDO>>();
		BaseServiceImpl<DocumentDO, OrderDocument> bi = new BaseServiceImpl<>();
		DocumentDO odo = new DocumentDO();

		OrderDocumentExample example = new OrderDocumentExample();
		Criteria create = example.createCriteria();
		if (query.getOrderSalesID() != null && query.getOrderSalesID() != 0) {
			create.andOrderSalesIDEqualTo(query.getOrderSalesID());
		}
		if (query.getFileName() != null && !"".equals(query.getFileName())) {
			create.andFileNameLike("%" + query.getFileName() + "%");
		}
		if (query.getId() != null && query.getId() != 0) {
			create.andIdEqualTo(query.getId());
		}
		if (query.getType() != null) {

			if (query.getType().equals(-1)) {
				create.andTypeNotEqualTo(0);
			} else {

				create.andTypeEqualTo(query.getType());
			}
		}
		if (query.getOrderID() != null) {
			create.andOrderIDEqualTo(query.getOrderID());
		}
		create.andIsDeleteEqualTo(0);
		try {
			if (query.isPage()) {
				example.setLimitByPage(query.getStart() + "," + query.getPageRows());
				long count = orderDocumentMapper.countByExample(example);
				result.setCount(count);
			}

			List<OrderDocument> list = orderDocumentMapper.selectByExample(example);

			List<DocumentDO> tList = bi.getTList(list, odo);

			ListIterator<DocumentDO> listIterator = tList.listIterator();

			while (listIterator.hasNext()) {
				DocumentDO next = listIterator.next();
				OrderHisSales od = orderDetailMapper.selectByPrimaryKey(next.getOrderSalesID());
				next.setCreatetimeTimeStamp(next.getCreatetime().getTime() + "");
				OOrder select = oOrderMapper.selectByPrimaryKey(next.getOrderID());

				next.setOrderNo(select.getOrderNO());
				
				
				if (od.getSessionID() !=null && od.getSessionID()!=0) {
					next.setDetailNo(select.getOrderNO() + "S1");
					next.setDocumentType("套系升级服务合同");
					next.setDocumentName("升级合同");
				}else if(od.getPaidBefore()!=null&&!od.getPaidBefore().equals(new BigDecimal(0))) {
					next.setDetailNo(select.getOrderNO() + "A1");
					next.setDocumentType("加选合同");
					next.setDocumentName("加选合同");
				}else {
					next.setDetailNo(select.getOrderNO());
					next.setDocumentType("套系拍摄服务合同");
					next.setDocumentName("订单合同");
				}
				
				OrderDocumentExample example1 = new OrderDocumentExample();
				Criteria create1 = example1.createCriteria();
				create1.andOrderSalesIDEqualTo(next.getOrderSalesID());
				create1.andOrderIDEqualTo(next.getOrderID());
				create1.andTypeNotEqualTo(0);
				create1.andIsDeleteEqualTo(0);
				
				List<OrderDocument> selectByExample = orderDocumentMapper.selectByExample(example1);
				List<DocumentDO> tList1 = bi.getTList(selectByExample, odo);
				
				//看里面的合同是否有签字的纯在
				for(DocumentDO d:tList1 ) {
					if(d.getType()==1&&d.getStatus()==1&&d.getIsDelete()==0) {
						next.setIsSign(1);
					}
				}
				
				if(next.getIsSign()==null) {
					next.setIsSign(0);
				}
				next.setAccessory(tList1);
			}
			result.setData(tList);
		} catch (Exception e) {
			logger.error(ResultDO.ORDER_GET_MSG_ERROR + "查询错误");
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_GET_MSG_ERROR + "查询错误");
			result.setErrorCode(ResultDO.ORDER_GET_ERROR);
		}
		return result;
	}

	@Override
	public ResultDO<DocumentDO> get(Integer id) {
		ResultDO<DocumentDO> result = new ResultDO<DocumentDO>();
		if (id == null || id == 0) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_GET_MSG_ERROR + "ID异常");
			result.setErrorCode(ResultDO.ORDER_GET_ERROR);
			return result;
		}
		BaseServiceImpl<DocumentDO, OrderDocument> bi = new BaseServiceImpl<>();
		DocumentDO odo = new DocumentDO();
		try {
			OrderDocument order = orderDocumentMapper.selectByPrimaryKey(id);
			DocumentDO t = bi.getT(order, odo);
			result.setData(t);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_GET_MSG_ERROR + "查询错误");
			result.setErrorCode(ResultDO.ORDER_GET_ERROR);
		}
		return result;
	}

	@Override
	public ResultDO<DocumentDO> modify(DocumentDO compact) {
		ResultDO<DocumentDO> result = new ResultDO<>();

		if (compact.getId() == null || compact.getId() == 0) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR + "id异常");
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			return result;
		}
		BaseServiceImpl<OrderDocument, DocumentDO> bi = new BaseServiceImpl<>();
		OrderDocument order = new OrderDocument();
		OrderDocument t = bi.getT(compact, order);
		t.setModifitime(new Date());
		int up = -1;
		try {
			up = orderDocumentMapper.updateByPrimaryKeySelective(t);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR);
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
		}
		if (up < 0) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR);
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
		}
		return result;
	}

	@Override
	public ResultDO<DocumentDO> remove(List<Integer> ids) {

		ResultDO<DocumentDO> result = new ResultDO<DocumentDO>();
		if (ids == null || ids.size() == 0) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR + "id异常");
			result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
			return result;
		}

		try {
			for (Integer id : ids) {
				OrderDocument oc = new OrderDocument();
				oc.setIsDelete(1);
				oc.setModifitime(new Date());
				oc.setId(id);
				orderDocumentMapper.updateByPrimaryKeySelective(oc);
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR);
			result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
		}
		return result;
	}

	@Override
	public ResultDO<DocumentDO> updateOrDeleteOrAdd(DocumentDO dd) {

		ResultDO<DocumentDO> result = new ResultDO<DocumentDO>();
		if (dd == null || dd.getId() == null) {
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR + "id异常");
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			result.setSuccess(false);
			return result;
		}
		ResultDO<DocumentDO> modify = this.modify(dd);

		try {
			if (!modify.isSuccess()) {
				throw new Exception();
			}
		} catch (Exception e) {
			logger.error(ResultDO.ORDER_MODIFY_MSG_ERROR + "修改失败");
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR + "修改失败");
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			result.setSuccess(false);
			return result;
		}

		List<DocumentDO> accessory = dd.getAccessory();
		ResultDO<DocumentDO> resultDO = this.get(dd.getId());
		DocumentDO documentD = null;
		try {
			if (resultDO.isSuccess()) {
				documentD = resultDO.getData();
			} else {
				throw new Exception();
			}
		} catch (Exception e) {
			logger.error(ResultDO.ORDER_MODIFY_MSG_ERROR + "修改失败");
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setSuccess(false);
			return result;
		}

		DocumentQueryDO query = new DocumentQueryDO();
		query.setOrderID(documentD.getOrderID());
		query.setOrderSalesID(documentD.getOrderSalesID());
		query.setType(-1);

		ResultDO<List<DocumentDO>> qeury = this.qeury(query);

		if (qeury.isSuccess()) {
			
			List<DocumentDO> model = qeury.getData();
			Iterator<DocumentDO> iterator = accessory.iterator();
			while (iterator.hasNext()) {
				DocumentDO next = iterator.next();
				Iterator<DocumentDO> iterator2 = model.listIterator();
				Integer x = 0;
				while (iterator2.hasNext()) {
					DocumentDO next2 = iterator2.next();
					if (next.getFileUrl().equals(next2.getFileUrl())) {
						x = 1;
						next2.setType(-1);
						continue;
					}
				}
				if (x.equals(0)) {
					next.setOrderID(documentD.getOrderID());
					next.setOrderSalesID(documentD.getOrderSalesID());
					next.setType(1);
					this.add(next);
				}
			}

			List<Integer> ids = new ArrayList<>();
			Iterator<DocumentDO> iterator2 = model.iterator();
			while (iterator2.hasNext()) {
				DocumentDO next = iterator2.next();
				if (!next.getType().equals(-1)) {
					ids.add(next.getId());
				}
			}
			if (ids.size() != 0) {
				ResultDO<DocumentDO> remove = this.remove(ids);
				if (!remove.isSuccess()) {
					logger.error(ResultDO.ORDER_DELETE_MSG_ERROR + "删除失败");
					result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR + "删除失败");
					result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
					result.setSuccess(false);
					return result;
				}
			}
		}
		return result;
	}

	@Override
	public ResultDO<Integer> addSignature(DocumentDO document) {
		// 1. 判断参数
		ResultDO<Integer> result = new ResultDO<>();
		if (null == document || null == document.getId() || null == document.getSignature()) {
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "对象转换失败");
			logger.error(ResultDO.ORDER_ADD_MSG_ERROR + "对象转换失败");
			result.setSuccess(false);
			return result;
		}

		// 2. 查询是否已经签字
		OrderDocument order = orderDocumentMapper.selectByPrimaryKey(document.getId());
		if (null == order) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			logger.error(ResultDO.ORDER_QUERY_ERROR + "合同模板查询失败");
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR + "合同模板查询失败");
			return result;
		}
		//如果合同已经生效不能在签名
		OrderDocumentExample example1 = new OrderDocumentExample();
		Criteria create1 = example1.createCriteria();
		create1.andOrderSalesIDEqualTo(order.getOrderSalesID());
		create1.andOrderIDEqualTo(order.getOrderID());
		create1.andTypeEqualTo(1);
		create1.andStatusEqualTo(1);
		create1.andIsDeleteEqualTo(0);
		List<OrderDocument> selectByExample = orderDocumentMapper.selectByExample(example1);
		if (null != selectByExample && selectByExample.size() >= 1) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR + "已存在正式合同信息");
			logger.error(ResultDO.ORDER_QUERY_MSG_ERROR + "已存在正式合同信息");
			return result;
		}

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
		String datepath=signedPdf+"/"+"date.png";
		boolean dateImage = ImageUtils.dateImage(sdf.format(new Date()), datepath);
		
		if(!dateImage) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR + "时间图片生成失败");
			logger.error(ResultDO.ORDER_QUERY_MSG_ERROR + "时间图片生成失败");
			return result;
		}
		
		// 3. 生成签字图片, 文件保存地址
		// 获取文件名及存放路径
		String fname = "合同签名";
		String path = signedPdf + "/";
		path = path + "sign" + System.currentTimeMillis() + ".png";
		try {
			// 生成图片
			if (!createImage(document.getSignature(), path)) {
				result.setSuccess(false);
				result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
				result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "图片处理错误1");
				logger.error(ResultDO.ORDER_ADD_ERROR + "图片处理错误1");
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			logger.error(ResultDO.ORDER_ADD_ERROR + "图片处理错误2");
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "图片处理错误2");
			return result;
		}

		// 添加签名后的合同
		// 签字前的合同
		String fileUrl = order.getFileUrl();
		// docx路径
		String replaceAll = fileUrl.replaceAll(".pdf", ".odt");
		String replaceAll2 = replaceAll.replaceAll(document1,source);

		
		
		// 签字后的合同
		String replaceFileInDocx = iDocx2PdfService.replaceFileInDocx(replaceAll2, path, "/Pictures/100002000000019300000076702D18DBDB433F2E.png");
		replaceFileInDocx=replaceFileInDocx.replaceAll("\\.pdf", ".odt");
		//datepath =
		replaceFileInDocx=iDocx2PdfService.replaceFileInDocx(replaceFileInDocx, datepath, "/Pictures/1000020100000190000000281F9DF3792A521354.png");
		
		String fileUrl1 = signedPdf + UUID.randomUUID().toString() + ".pdf";

		boolean sign = iDigitallySigningPdfService.sign(replaceFileInDocx, fileUrl1, "/Im19",
				keyStorePath, passWord,sealPicPath);

		if (sign) {

			//上传到文件管理
			/*FileManagementDO fileMangement=new FileManagementDO();
			fileMangement.setOrigName("套系拍摄服务合同");
			fileMangement.setCategory("contract");
			fileMangement.setOrigFile(fileUrl1);
			com.jingyanzi.sys.domain.result.ResultDO add211 = getIFileManagementService().add(fileMangement);
			if(add211.isSuccess()) {
				fileMangement = (FileManagementDO) add211.getModel(com.jingyanzi.sys.domain.result.ResultDO.FIRST_MODEL_KEY);
			}else {
				result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR+"文件生成出错");
				logger.error(ResultDO.ORDER_ADD_MSG_ERROR+"文件生成出错");
				result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			}
			fileUrl1=fileMangement.getUrl();*/
			
			fileUrl1 = fileUrl1.replace(source,document1);
			
			DocumentDO compact = new DocumentDO();
		//	compact.setFileID(fileMangement.getId);
			compact.setOrderID(order.getOrderID());
			compact.setOrderSalesID(order.getOrderSalesID());
			compact.setType(1);
			compact.setStatus(1);
			compact.setFileName(order.getFileName() + "（有效）");
			compact.setFileUrl(fileUrl1);
			compact.setDocumentNo(UUID.randomUUID().toString());
			ResultDO<Integer> res = add(compact);

			if (!res.isSuccess()) {
				result.setSuccess(false);
				result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
				result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "附件db保存错误");
				logger.error(ResultDO.ORDER_ADD_MSG_ERROR + "附件db保存错误");
				return result;
			}
		}else {
			
			result.setSuccess(false);
			logger.error(ResultDO.ORDER_ADD_MSG_ERROR + "附件db保存错误");
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "附件db保存错误");
			return result;
		}
		// 4. 保存图片附件至数据库
		DocumentDO compact1 = new DocumentDO();
		compact1.setOrderID(order.getOrderID());
		compact1.setOrderSalesID(order.getOrderSalesID());
		compact1.setType(1);
		compact1.setStatus(0);
		compact1.setFileName(fname);
		path=path.replace(source,document1);
		compact1.setFileUrl(path);
		compact1.setDocumentNo(UUID.randomUUID().toString());
		ResultDO<Integer> res = add(compact1);
		if (!res.isSuccess()) {
			result.setSuccess(false);
			logger.error(ResultDO.ORDER_QUERY_MSG_ERROR + "附件db保存错误");
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR + "附件db保存错误");
			return result;
		}
		// TODO: 5. 返回合同下载地址
		return result;
	}


	// Base64解码,对字节数组字符串进行Base64解码并生成图片
	public static boolean createImage(String imgStr, String filePath) throws Exception {

		BASE64Decoder decoder = new BASE64Decoder();
		// 去掉前缀
		String img = imgStr.substring(imgStr.indexOf(",") + 1, imgStr.length());
		byte[] b = decoder.decodeBuffer(img);
		for (int i = 0; i < b.length; ++i) {
			if (b[i] < 0) {// 调整异常数据
				b[i] += 256;
			}
		}
		// 生成图片
		OutputStream out = new FileOutputStream(filePath);
		out.write(b);
		out.flush();
		out.close();

		try {
			File file = new File(filePath);
			Image imgFile = ImageIO.read(file);
			BufferedImage des = Rotate(imgFile, 270);
			ImageIO.write(des, "png", file);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return true;
	}

	// 图片旋转
	public static BufferedImage Rotate(Image src, int angel) {
		int src_width = src.getWidth(null);
		int src_height = src.getHeight(null);
		// 计算尺寸
		Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);

		BufferedImage res = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_4BYTE_ABGR);
		Graphics2D g2 = res.createGraphics();
		g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2);
		g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
		g2.drawImage(src, null, null);
		return res;
	}

	// 计算旋转后尺寸
	public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
		// if angel is greater than 90 degree, we need to do some conversion
		if (angel >= 90) {
			if (angel / 90 % 2 == 1) {
				int temp = src.height;
				src.height = src.width;
				src.width = temp;
			}
			angel = angel % 90;
		}

		double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
		double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
		double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
		double angel_dalta_width = Math.atan((double) src.height / src.width);
		double angel_dalta_height = Math.atan((double) src.width / src.height);

		int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width));
		int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height));
		int des_width = src.width + len_dalta_width * 2;
		int des_height = src.height + len_dalta_height * 2;
		return new java.awt.Rectangle(new Dimension(des_width, des_height));
	}

	@Override
	public ResultDO<Integer> removeDocument(Integer orderId,String name) {
		ResultDO<Integer> result=new ResultDO<>();
		if(orderId==null||orderId==0) {
			result.setErrorCode(ResultDO.ORDER_ID_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ID_MSG_ERROR+"订单id异常");
			return result;
		}
		
		OrderDocumentExample example=new OrderDocumentExample();
		example.createCriteria().andOrderIDEqualTo(orderId).andIsDeleteEqualTo(0);
		
		try {
			
			List<OrderDocument> selectByExample = orderDocumentMapper.selectByExample(example);
			for(OrderDocument od:selectByExample) {
				
				od.setModifier(name);
				od.setIsDelete(1);
				int update = orderDocumentMapper.updateByPrimaryKeySelective(od);
				if(update<0) {
					throw new Exception();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			logger.error(ResultDO.ORDER_MODIFY_MSG_ERROR+"合同更新失败");
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR+"合同更新失败");
			return result;
		}
		return result;
	}
	
	
}
